home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / PR8ADPL7.TAR / productivity_tools / PR8ADPL7 / LoginWindow.java < prev    next >
Encoding:
Java Source  |  1996-05-23  |  2.2 KB  |  113 lines

  1. // LoginWindow.java
  2. // A popup window to prompt for a username and password
  3. import java.awt.*;
  4.  
  5. public class LoginWindow extends FixedFrame
  6. {
  7.     Component parent;
  8.     Button ok, can;
  9.     TextField name, pass;
  10.  
  11.     LoginWindow(Component p)
  12.     {
  13.     super(new Dimension(250, -1));
  14.     parent = p;
  15.     setLayout(new BorderLayout());
  16.  
  17.     Panel top = new Panel();
  18.     top.setLayout(new BorderLayout());
  19.     Panel left = new Panel();
  20.     left.setLayout(new GridLayout(2,1));
  21.     left.add(new Label("Username"));
  22.     left.add(new Label("Password"));
  23.     top.add("West",left);
  24.     Panel right = new Panel();
  25.     right.setLayout(new GridLayout(2,1));
  26.     right.add(name = new TextField());
  27.     right.add(pass = new TextField());
  28.     pass.setEchoCharacter('*');
  29.     top.add("Center",right);
  30.     add("North",top);
  31.  
  32.     Panel but = new Panel();
  33.     but.setLayout(new FlowLayout(FlowLayout.RIGHT));
  34.     but.add(ok = new Button("Ok"));
  35.     but.add(can = new Button("Cancel"));
  36.     add("South",but);
  37.  
  38.     setTitle("JFS login");
  39.     setResizable(false);
  40.     pack();
  41.     show();
  42.     }
  43.  
  44.     String getname()
  45.     {
  46.     return name.getText();
  47.     }
  48.  
  49.     String getpass()
  50.     {
  51.     return pass.getText();
  52.     }
  53.  
  54.     public boolean handleEvent(Event evt)
  55.     {
  56.     if (evt.id == Event.ACTION_EVENT) {
  57.         if (evt.target == name)
  58.             pass.requestFocus();
  59.         else if (evt.target == ok || evt.target == pass) {
  60.             parent.postEvent(
  61.                 new Event(this, Event.ACTION_EVENT, "Ok"));
  62.             dispose();
  63.             }
  64.         }
  65.     else if (evt.target == can || evt.id == Event.WINDOW_DESTROY) {
  66.         parent.postEvent(new Event(this, Event.ACTION_EVENT, "Cancel"));
  67.         dispose();
  68.         }
  69.     return super.handleEvent(evt);
  70.     }
  71. }
  72.  
  73. // MessageWindow
  74. // A useful class for creating pop-up messages
  75. class MessageWindow extends FixedFrame
  76. {
  77.     Button ok;
  78.     MultiLabel lab;
  79.  
  80.     MessageWindow(String title, String msg)
  81.     {
  82.     setLayout(new BorderLayout());
  83.     add("Center",lab = new MultiLabel(msg));
  84.     Panel bp = new Panel();
  85.     bp.setLayout(new FlowLayout());
  86.     bp.add(ok = new Button("Ok"));
  87.     add("South",bp);
  88.  
  89.     setTitle(title);
  90.     setResizable(false);
  91.     pack();
  92.     show();
  93.     }
  94.  
  95.     public boolean action(Event evt, Object obj)
  96.     {
  97.     if (evt.target == ok) dispose();
  98.     return true;
  99.     }
  100. }
  101.  
  102.  
  103. // ErrorWindow
  104. // A window for displaying pop-up error messages
  105. class ErrorWindow extends MessageWindow
  106. {
  107.     ErrorWindow(String m)
  108.     {
  109.     super("Error",m);
  110.     }
  111. }
  112.  
  113.